package cn.tedu._04weibo.controller;


import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentAddParam;
import cn.tedu._04weibo.pojo.vo.CommentVO;
import cn.tedu._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.tedu._04weibo.pojo.entity.Comment;

import java.util.Date;
import java.util.List;


@RestController
@Slf4j
@Tag(name = "03-评论管理接口")
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @Operation(summary = "发布评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insertComment(@RequestBody CommentAddParam commentAddParam, HttpSession session){
        log.debug("传过来的评论内容："+commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        int row=commentMapper.insertByContent(comment);
        return JsonResult.ok();
    }

    @GetMapping("SelectByWeiboId")
    @Operation(summary = "评论列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectByWeiboId(Long weiboId){
        log.debug("传过来的微博id："+weiboId);
        List<CommentVO> list=commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(list);
    }
}
